﻿namespace PMS.Client.FrmUserMove
{
    using PMS.BLL;
    using PMS.Common;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;

    public class FrmMoveOut : Form
    {
        private Button btnCancel;
        private Button btnOK;
        private IContainer components = null;
        private GroupBox groupBox1;
        private Label label1;
        private Label label2;
        private Label label24;
        private Label label25;
        private Label label26;
        private Label label45;
        private Label lbTitle;
        private UserChangeLogBLL logbll = new UserChangeLogBLL();
        private OrganBLL organBLL = new OrganBLL();
        private ImageList organTreeImageList;
        private TreeNode selectedNode = null;
        private TreeView treeView1;
        private TextBox txtCurrent;
        private DateTimePicker txtDate;
        private TextBox txtNo;
        private TextBox txtRemark;
        private TextBox txtSelected;
        private TextBox txtSummary;
        private User user = null;
        private UserBLL userbll = new UserBLL();
        private int userid = 0;

        public FrmMoveOut(int userid)
        {
            this.InitializeComponent();
            this.userid = userid;
            this.user = this.userbll.GetModel(userid);
            this.txtCurrent.Text = this.user.RegistTo;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int? organID = this.user.OrganID;
            int num = int.Parse(this.selectedNode.Name);
            if ((!((organID.GetValueOrDefault() == num) && organID.HasValue) || (MessageBox.Show("未改变机构，确认继续？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)) && (MessageBox.Show("确定把用户 " + this.user.UserName + " 从【" + this.user.RegistTo + "】划转到【" + this.selectedNode.Text + "】", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK))
            {
                UserChangeLog log2 = new UserChangeLog {
                    RegistTo = this.selectedNode.Text,
                    OrganID = new int?(int.Parse(this.selectedNode.Name)),
                    RegistNo = this.txtNo.Text,
                    RegistDate = new DateTime?(this.txtDate.Value),
                    RegistSummary = this.txtSummary.Text,
                    RegistRemark = this.txtRemark.Text,
                    UserID = this.userid,
                    UpdateDate = new DateTime?(DateTime.Now),
                    UpdateUser = PMS.Common.Keys.Admin.LoginName,
                    LogType = "调动",
                    CheckState = "未审核"
                };
                UserChangeLog model = log2;
                if (new UserChangeLogBLL().Add(model) > 0)
                {
                    MessageBox.Show("划转成功，等待审核中！");
                    base.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("划转失败！");
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmMoveOut_Load(object sender, EventArgs e)
        {
            this.treeView1.ImageList = this.organTreeImageList;
            this.ReturnOrganBind(this.organBLL.GetList(), 0, this.treeView1.Nodes);
            this.treeView1.Nodes[0].Expand();
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmMoveOut));
            this.treeView1 = new TreeView();
            this.groupBox1 = new GroupBox();
            this.txtDate = new DateTimePicker();
            this.label45 = new Label();
            this.txtRemark = new TextBox();
            this.label26 = new Label();
            this.txtSummary = new TextBox();
            this.label25 = new Label();
            this.txtNo = new TextBox();
            this.label24 = new Label();
            this.txtCurrent = new TextBox();
            this.label2 = new Label();
            this.txtSelected = new TextBox();
            this.label1 = new Label();
            this.btnCancel = new Button();
            this.btnOK = new Button();
            this.organTreeImageList = new ImageList(this.components);
            this.lbTitle = new Label();
            this.groupBox1.SuspendLayout();
            base.SuspendLayout();
            this.treeView1.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.treeView1.BorderStyle = BorderStyle.FixedSingle;
            this.treeView1.Dock = DockStyle.Top;
            this.treeView1.FullRowSelect = true;
            this.treeView1.HideSelection = false;
            this.treeView1.ItemHeight = 0x10;
            this.treeView1.Location = new Point(0, 0x1f);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new Size(0x20c, 0x15a);
            this.treeView1.TabIndex = 1;
            this.treeView1.AfterSelect += new TreeViewEventHandler(this.treeView1_AfterSelect);
            this.groupBox1.Controls.Add(this.txtDate);
            this.groupBox1.Controls.Add(this.label45);
            this.groupBox1.Controls.Add(this.txtRemark);
            this.groupBox1.Controls.Add(this.label26);
            this.groupBox1.Controls.Add(this.txtSummary);
            this.groupBox1.Controls.Add(this.label25);
            this.groupBox1.Controls.Add(this.txtNo);
            this.groupBox1.Controls.Add(this.label24);
            this.groupBox1.Controls.Add(this.txtCurrent);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.txtSelected);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new Point(8, 0x17f);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(0x1fc, 0x98);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.txtDate.CalendarMonthBackground = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtDate.Location = new Point(0x15c, 0x45);
            this.txtDate.Name = "txtDate";
            this.txtDate.Size = new Size(0x87, 0x15);
            this.txtDate.TabIndex = 0x58;
            this.label45.AutoSize = true;
            this.label45.Location = new Point(0x121, 0x49);
            this.label45.Name = "label45";
            this.label45.Size = new Size(0x35, 12);
            this.label45.TabIndex = 0x57;
            this.label45.Text = "下文时间";
            this.txtRemark.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtRemark.BorderStyle = BorderStyle.FixedSingle;
            this.txtRemark.Location = new Point(0x53, 0x7b);
            this.txtRemark.Name = "txtRemark";
            this.txtRemark.Size = new Size(400, 0x15);
            this.txtRemark.TabIndex = 0x56;
            this.label26.AutoSize = true;
            this.label26.Location = new Point(0x30, 0x7d);
            this.label26.Name = "label26";
            this.label26.Size = new Size(0x1d, 12);
            this.label26.TabIndex = 0x55;
            this.label26.Text = "备注";
            this.txtSummary.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtSummary.BorderStyle = BorderStyle.FixedSingle;
            this.txtSummary.Location = new Point(0x53, 0x60);
            this.txtSummary.Name = "txtSummary";
            this.txtSummary.Size = new Size(400, 0x15);
            this.txtSummary.TabIndex = 0x54;
            this.label25.AutoSize = true;
            this.label25.Location = new Point(0x30, 0x62);
            this.label25.Name = "label25";
            this.label25.Size = new Size(0x1d, 12);
            this.label25.TabIndex = 0x53;
            this.label25.Text = "摘要";
            this.txtNo.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtNo.BorderStyle = BorderStyle.FixedSingle;
            this.txtNo.Location = new Point(0x53, 0x45);
            this.txtNo.Name = "txtNo";
            this.txtNo.Size = new Size(200, 0x15);
            this.txtNo.TabIndex = 0x52;
            this.label24.AutoSize = true;
            this.label24.Location = new Point(0x30, 0x49);
            this.label24.Name = "label24";
            this.label24.Size = new Size(0x1d, 12);
            this.label24.TabIndex = 0x51;
            this.label24.Text = "文号";
            this.txtCurrent.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtCurrent.BorderStyle = BorderStyle.FixedSingle;
            this.txtCurrent.Location = new Point(0x53, 0x2a);
            this.txtCurrent.Name = "txtCurrent";
            this.txtCurrent.ReadOnly = true;
            this.txtCurrent.Size = new Size(200, 0x15);
            this.txtCurrent.TabIndex = 0x34;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(12, 0x2c);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x41, 12);
            this.label2.TabIndex = 0x33;
            this.label2.Text = "您当前所在";
            this.txtSelected.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtSelected.BorderStyle = BorderStyle.FixedSingle;
            this.txtSelected.Location = new Point(0x53, 15);
            this.txtSelected.Name = "txtSelected";
            this.txtSelected.Size = new Size(200, 0x15);
            this.txtSelected.TabIndex = 50;
            this.label1.AutoSize = true;
            this.label1.Location = new Point(12, 0x11);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "您选择的是";
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x120, 0x21d);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 9;
            this.btnCancel.Text = "关闭";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnOK.BackColor = Color.White;
            this.btnOK.FlatStyle = FlatStyle.Popup;
            this.btnOK.Location = new Point(0x5f, 0x21d);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new Size(0x4b, 0x17);
            this.btnOK.TabIndex = 8;
            this.btnOK.Text = "确定";
            this.btnOK.UseVisualStyleBackColor = false;
            this.btnOK.Click += new EventHandler(this.btnOK_Click);
            this.organTreeImageList.ImageStream = (ImageListStreamer) manager.GetObject("organTreeImageList.ImageStream");
            this.organTreeImageList.TransparentColor = Color.Transparent;
            this.organTreeImageList.Images.SetKeyName(0, "ADMIN.png");
            this.organTreeImageList.Images.SetKeyName(1, "business.png");
            this.lbTitle.BackColor = Color.Gold;
            this.lbTitle.Dock = DockStyle.Top;
            this.lbTitle.Font = new Font("宋体", 14.25f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.lbTitle.ForeColor = Color.Black;
            this.lbTitle.Location = new Point(0, 0);
            this.lbTitle.Name = "lbTitle";
            this.lbTitle.Size = new Size(0x20c, 0x1f);
            this.lbTitle.TabIndex = 0x38;
            this.lbTitle.Text = "人员划转到其它部门";
            this.lbTitle.TextAlign = ContentAlignment.MiddleCenter;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x20c, 0x23c);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.Controls.Add(this.groupBox1);
            base.Controls.Add(this.treeView1);
            base.Controls.Add(this.lbTitle);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmMoveOut";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "人员划转到其它部门";
            base.Load += new EventHandler(this.FrmMoveOut_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            base.ResumeLayout(false);
        }

        private void ReturnOrganBind(List<Organ> organList, int OrganID, TreeNodeCollection treeNodes)
        {
            List<Organ> list = organList.FindAll(delegate (Organ f) {
                int? parentID = f.ParentID;
                int organID = OrganID;
                return (parentID.GetValueOrDefault() == organID) && parentID.HasValue;
            }).ToList<Organ>();
            if (list.Count > 0)
            {
                TreeNode node = null;
                foreach (Organ organ in list)
                {
                    node = new TreeNode {
                        Text = organ.OrganName,
                        ImageIndex = organ.IsOffice - 1,
                        SelectedImageIndex = organ.IsOffice - 1,
                        Name = organ.OrganID.ToString(),
                        Tag = organ.LevelNo
                    };
                    treeNodes.Add(node);
                    this.ReturnOrganBind(organList, organ.OrganID, node.Nodes);
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.selectedNode = this.treeView1.SelectedNode;
            this.txtSelected.Text = this.selectedNode.Text;
        }
    }
}

